<!--

/*
** Copyright (c) 2015 The Khronos Group Inc.
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and/or associated documentation files (the
** "Materials"), to deal in the Materials without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Materials, and to
** permit persons to whom the Materials are furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be included
** in all copies or substantial portions of the Materials.
**
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/

-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebGL getBufferSubData test.</title>
<link rel="stylesheet" href="../../resources/js-test-style.css"/>
<script src="../../js/js-test-pre.js"></script>
<script src="../../js/webgl-test-utils.js"></script>
</head>
<body>
<div id="description"></div>
<div id="console"></div>
<script>
"use strict";
description("This test makes sure that getBufferSubData acts as expected governed by WebGL 2.");

var wtu = WebGLTestUtils;

var gl = wtu.create3DContext(undefined, undefined, 2);

var vertices = [
     1.1,  1.0,  1.3,
    -1.0, -1.0,  -5.0,
     5.3, -1.0,  1.0
];
var floatArray = new Float32Array(vertices);

wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");

var buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, floatArray, gl.STATIC_DRAW);

debug("");
debug("Test that getBufferSubData successfully works reading buffer data from gl.ARRAY_BUFFER");
var retArray = new Float32Array(vertices.length);
gl.getBufferSubData(gl.ARRAY_BUFFER, 0, retArray);
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "gl.getBufferSubData(gl.ARRAY_BUFFER, 0, arrBuffer) should WORK");

debug("Check array data to match original data set by the buffer");
var failed = false;
for (var i = 0; i < vertices.length; i++) {
  if (floatArray[i] != retArray[i]) {
    failed = true;
    break;
  }
}
if (failed)
  testFailed("The returned array buffer fails to match original data");
else
  testPassed("The returned array buffer matches original data");

debug("Test that getBufferSubData fails when given a buffer with its size larger than the original data");
var extraLargeBuffer = new Uint8Array(vertices.length * Float32Array.BYTES_PER_ELEMENT + 1);
gl.getBufferSubData(gl.ARRAY_BUFFER, 0, extraLargeBuffer);
wtu.glErrorShouldBe(gl, gl.INVALID_VALUE, "gl.getBufferSubData(gl.ARRAY_BUFFER, 0, extraLargeBuffer) with extra length should generate INVALID_VALUE");

debug("Test that getBufferSubData fails when offset summed with buffer length is larger than the size of the original data size");
gl.getBufferSubData(gl.ARRAY_BUFFER, vertices.length * Float32Array.BYTES_PER_ELEMENT + 1, retArray);
wtu.glErrorShouldBe(gl, gl.INVALID_VALUE, "gl.getBufferSubData(gl.ARRAY_BUFFER, vertices.length * Float32Array.BYTES_PER_ELEMENT + 1, arrBuffer) with offset larger than original data size should generate INVALID_VALUE");
gl.getBufferSubData(gl.ARRAY_BUFFER, 1, retArray);
wtu.glErrorShouldBe(gl, gl.INVALID_VALUE, "gl.getBufferSubData(gl.ARRAY_BUFFER, 1, arrBuffer) with offset and length larger than buffer size should generate INVALID_VALUE");

debug("Test that getBufferSubData fails when 0 is bound to the target");
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
gl.getBufferSubData(gl.ELEMENT_ARRAY_BUFFER, 0, retArray);
wtu.glErrorShouldBe(gl, gl.INVALID_OPERATION, "gl.getBufferSubData(gl.ELEMENT_ARRAY_BUFFER, 0, arrBuffer) should generate INVALID_OPERATION");

debug("Test that getBufferSubData fails when offset is less than 0");
gl.getBufferSubData(gl.ARRAY_BUFFER, -1, retArray);
wtu.glErrorShouldBe(gl, gl.INVALID_VALUE, "gl.getBufferSubData(gl.ARRAY_BUFFER, -1, arrBuffer) should generate INVALID_VALUE");

finishTest();

var successfullyParsed = true;
</script>
</body>
</html>
